package com.kevin.controller.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kevin.common.api.PageDTO;
import com.kevin.common.api.R;
import com.kevin.service.entity.SysDictData;
import com.kevin.service.service.SysDictDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典数据
 *
 * @author 阿沐 babamu@126.com
 */
@RestController
@RequestMapping("sys/dict/data")
@Api(tags = "字典数据")
@AllArgsConstructor
public class SysDictDataController {
    private final SysDictDataService sysDictDataService;

    @GetMapping("page")
    @ApiOperation(value = "分页")
    @SaCheckPermission(value = "sys:dict:page")
    public R<PageInfo<SysDictData>> page(PageDTO pageDTO, Long dictTypeId) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return R.ok(new PageInfo<>(sysDictDataService.list(Wrappers.lambdaQuery(SysDictData.builder().dictTypeId(dictTypeId).build()))));
    }

    @GetMapping("{id}")
    @ApiOperation(value = "信息")
    @SaCheckPermission(value = "sys:dict:info")
    public R<SysDictData> get(@PathVariable("id") Long id) {
        return R.ok(sysDictDataService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "保存")
    @SaCheckPermission(value = "sys:dict:save")
    public R<String> save(@RequestBody @Valid SysDictData vo) {
        Assert.notNull(null, "暂不支持新增");
        sysDictDataService.save(vo);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @SaCheckPermission(value = "sys:dict:update")
    public R<String> update(@RequestBody @Valid SysDictData vo) {
        sysDictDataService.updateById(vo);

        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission(value = "sys:dict:delete")
    public R<String> delete(@RequestBody List<Long> idList) {
        sysDictDataService.removeByIds(idList);
        return R.ok();
    }

}
